(function () {
    'use strict';

    angular.module('KingAdmin.pages.pdev.devctrl')
        .factory('DevctrlService', DevctrlService);

    /** @ngInject */
    function DevctrlService($resource,toastr,CommonService) {

        var rest = $resource('pdev/devctrl/:id', {}, {
            'create': {method: 'POST'},
            'update': {method: 'PUT'},
        });
        
        function getSmartData(param,callback) {
            $resource('pdev/devctrl/getSmartData', {}, {
                'query': {method: 'POST'}
            }).query(param,
                function (data,hears) {
                    console.log(data);
                    if(angular.isFunction(callback)) callback(data);callback(data)
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                });
        }
        function del(param,callback) {
            CommonService.danger('确定删除?', function () {
                rest.delete(param,
                    function (data) {
                        if (data.code == 0) {
                            toastr.success("删除成功！", "提示", {"progressBar": true,});
                        } else {
                            toastr.warning("删除失败！", "提示", {"progressBar": true,});
                        }
                        if(angular.isFunction(callback)) callback(data);
                    }, function (error) {
                        toastr.error(error, "提示", {"progressBar": true,});
                    })
            })
        }
        function save(param,callback) {
            CommonService.info('确定保存?',function () {
                if(angular.isDefined(param.id)&&param.id!=null){
                    rest.update(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功","提示",{"progressBar": true,});
                            if(angular.isFunction(callback)) callback(data);
                        }, function (error) {
                            toastr.error(error,"提示",{"progressBar": true,});
                        })
                }else{
                    rest.create(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功","提示",{"progressBar": true,});
                            if(angular.isFunction(callback)) callback(data);
                        }, function (error) {
                            toastr.error(error,"提示",{"progressBar": true,});
                        })
                }
            });
        }
        function getInfo(param,callback) {
            rest.get(param,
                function (data) {
                    console.log(data);
                    if(angular.isFunction(callback)) callback(data);
                }, function (error) {
                    toastr.error(error,"提示",{"progressBar": true,});
                })
        }

        // 设备控制
        function ctrl(param, title,callback) {
            CommonService.danger('确定要进行' + title + '操作?', function () {
                $resource('pdev/devctrl/ctrl', param, {
                    'addPriv': {method: 'POST'}
                }).addPriv({},
                    function (data) {
                        if (data.code == 0) {
                            toastr.success(title + "操作成功！", "提示", {"progressBar": true,});
                        } else {
                            toastr.warning(title + "操作失败！", "提示", {"progressBar": true,});
                        }
                        console.log(data);
                        callback(data)
                    }, function (error) {
                        toastr.error(error, "提示", {"progressBar": true,});
                    });
                });
        }
        // 设备控制
        function ctrlRead(param, title,callback) {
            $resource('pdev/devctrl/ctrl', param, {
                    'addPriv': {method: 'POST'}}).addPriv({},
                    function (data) {
                        if (data.code == 0) {
                            toastr.success(title + "操作成功！", "提示", {"progressBar": true,});
                        } else {
                            toastr.warning(title + "操作失败！", "提示", {"progressBar": true,});
                        }
                        console.log(data);
                        callback(data)
                    }, function (error) {
                        toastr.error(error, "提示", {"progressBar": true,});
                    });
        }

        function buildReadOpt (devUid, optMsg) {
            var sendStrLen = 13 + (optMsg.length / 2);
            var sendPrevStr = 'AA04' +
                intToHex(sendStrLen, 4) +
                devUid +
                optMsg +
                '0000' +
                'BB';
            return sendPrevStr;
        }

        function buildWriteOpt (devUid, optMsg) {
            var sendStrLen = 13 + (optMsg.length / 2);
            console.log(sendStrLen);
            console.log(sendStrLen);
            var sendPrevStr = 'AA05' +
                intToHex(sendStrLen, 4) +
                devUid +
                optMsg +
                '0000' +
                'BB';
            return sendPrevStr;
        }

        function intToHex(val, len) {
            var hexStr = val.toString(16);
            var zeroStr = '';

            for (var i=0; i < len - hexStr.length; i++) {
                zeroStr += '0';
            }
            return zeroStr + hexStr;
        }
        
        return {
            getSmartData:getSmartData,
            del:del,
            save:save,
            getInfo:getInfo,
            ctrl: ctrl,
            ctrlRead: ctrlRead,
            buildWriteOpt: buildWriteOpt,
            buildReadOpt: buildReadOpt
        };
    }

})();
